package zzhyc.keystore.sigmob

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.sigmob.windad.WindAdError
import com.sigmob.windad.WindAds
import com.sigmob.windad.natives.WindNativeAdData
import com.sigmob.windad.natives.WindNativeAdRequest
import com.sigmob.windad.natives.WindNativeUnifiedAd
import com.sigmob.windad.newInterstitial.WindNewInterstitialAd
import com.sigmob.windad.newInterstitial.WindNewInterstitialAdListener
import com.sigmob.windad.newInterstitial.WindNewInterstitialAdRequest
import com.sigmob.windad.rewardVideo.WindRewardAdRequest
import com.sigmob.windad.rewardVideo.WindRewardInfo
import com.sigmob.windad.rewardVideo.WindRewardVideoAd
import com.sigmob.windad.rewardVideo.WindRewardVideoAdListener
import zzhyc.keystore.R
import zzhyc.keystore.databinding.ActivitySigmobInfoBinding

private const val TAG = "SigmobInfoActivity"

class SigmobInfoActivity : AppCompatActivity() {

    private val binding by lazy { ActivitySigmobInfoBinding.inflate(layoutInflater) }
    private var logStr = StringBuilder()
    private var videoAd: WindRewardVideoAd? = null
    private var interstitialAd: WindNewInterstitialAd? = null
    private var nativeUnifiedAd: WindNativeUnifiedAd? = null

    companion object {
        fun startActivity(context: Context) {
            val intent = Intent(context, SigmobInfoActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        //激励按钮
        binding.sigmobInfoBtnJiLi.setOnClickListener {
            initJiLi()
        }

        //插屏
        binding.sigmobInfoBtnCha.setOnClickListener {
            initCha()
        }

        //信息流
        binding.sigmobInfoBtnDraw.setOnClickListener {
//            initYuanSheng()
        }
    }

    private fun initYuanSheng() {
        val adRequest = WindNativeAdRequest(SigmobKey.SIGMOB_INFO_ID, "", null)
        nativeUnifiedAd = WindNativeUnifiedAd(adRequest)
        nativeUnifiedAd?.setNativeAdLoadListener(object :
            WindNativeUnifiedAd.WindNativeAdLoadListener {
            override fun onAdError(error: WindAdError?, placementId: String?) {
                setLogsView("原声广告/onAdError: ${error?.errorCode}，msg-- ${error?.message} \n")
            }

            override fun onAdLoad(
                adDataList: MutableList<WindNativeAdData>?, placementId: String?
            ) {
                setLogsView("原声广告/onAdLoad: size-- ${adDataList?.size}")
                if (adDataList != null && adDataList.isNotEmpty()) {
                    val nativeAd = adDataList[0]
                }
            }
        })

        nativeUnifiedAd?.loadAd(1)
    }

    private fun initCha() {
        val adRequest = WindNewInterstitialAdRequest(SigmobKey.SIGMOB_CHA_ID, "", null)
        interstitialAd = WindNewInterstitialAd(adRequest)
        interstitialAd?.setWindNewInterstitialAdListener(object : WindNewInterstitialAdListener {
            override fun onInterstitialAdLoadSuccess(placementId: String?) {
                setLogsView("插屏广告/onInterstitialAdLoadSuccess:广告加载成功 \n")

                if (interstitialAd?.isReady == true) {
                    val showOption = HashMap<String, String>()

                    showOption[WindAds.AD_SCENE_ID] = "插屏场景ID"
                    interstitialAd?.show(showOption)
                }
            }

            override fun onInterstitialAdPreLoadSuccess(placementId: String?) {
                setLogsView("插屏广告/onInterstitialAdPreLoadSuccess:广告服务填充成功 \n")
            }

            override fun onInterstitialAdPreLoadFail(placementId: String?) {
                setLogsView("插屏广告/onInterstitialAdPreLoadFail: \n")
            }

            override fun onInterstitialAdShow(placementId: String?) {
                setLogsView("插屏广告/onInterstitialAdShow: \n")
            }

            override fun onInterstitialAdClicked(placementId: String?) {
                setLogsView("插屏广告/onInterstitialAdClicked: \n")
            }

            override fun onInterstitialAdClosed(placementId: String?) {
                setLogsView("插屏广告/onInterstitialAdClosed: 奖励-- ${interstitialAd?.ecpm}分 \n")
            }

            override fun onInterstitialAdLoadError(error: WindAdError?, placementId: String?) {
                setLogsView("插屏广告/onInterstitialAdLoadError: ${error?.errorCode}，msg-- ${error?.message} \n")
            }

            override fun onInterstitialAdShowError(error: WindAdError?, placementId: String?) {
                setLogsView("插屏广告/onInterstitialAdShowError: ${error?.errorCode}，msg-- ${error?.message} \n")
            }
        })
        interstitialAd?.loadAd()
    }

    private fun initJiLi() {
        val adRequest = WindRewardAdRequest(SigmobKey.SIGMOB_JILI_ID, "", null)
        videoAd = WindRewardVideoAd(adRequest)
        videoAd?.setWindRewardVideoAdListener(object : WindRewardVideoAdListener {
            override fun onRewardAdLoadSuccess(placementId: String?) {
                setLogsView("激励视频/onRewardAdLoadSuccess/广告物料加载成功  \n")
                if (videoAd?.isReady == true) {
                    val showOption = HashMap<String, String>()

                    showOption[WindAds.AD_SCENE_ID] = "场景ID"
                    videoAd?.show(showOption)
                }
            }

            override fun onRewardAdPreLoadSuccess(placementId: String?) {
                setLogsView("激励视频/onRewardAdPreLoadSuccess/广告填充成功: \n")
            }

            override fun onRewardAdPreLoadFail(placementId: String?) {
                setLogsView("激励视频/onRewardAdPreLoadFail: \n")
            }

            override fun onRewardAdPlayStart(placementId: String?) {
                setLogsView("激励视频/onRewardAdPlayStart: \n")
            }

            override fun onRewardAdPlayEnd(placementId: String?) {
                setLogsView("激励视频/onRewardAdPlayEnd: \n")
            }

            override fun onRewardAdClicked(placementId: String?) {
                setLogsView("激励视频/onRewardAdClicked: \n")
            }

            override fun onRewardAdClosed(placementId: String?) {
                setLogsView("激励视频/onRewardAdClosed:  奖励-- ${videoAd?.ecpm}分 \n")
            }

            override fun onRewardAdRewarded(rewardInfo: WindRewardInfo?, placementId: String?) {
                setLogsView("激励视频/onRewardAdRewarded: \n")
            }

            override fun onRewardAdLoadError(error: WindAdError?, placementId: String?) {
                setLogsView("激励视频/onRewardAdLoadError: ${error?.errorCode}，msg-- ${error?.message} \n")
            }

            override fun onRewardAdPlayError(error: WindAdError?, placementId: String?) {
                setLogsView("激励视频/onRewardAdPlayError: ${error?.errorCode}，msg-- ${error?.message} \n")
            }
        })
        val isLoad = videoAd?.loadAd()
        setLogsView("激励视频/initJiLi:isLoad-- ${isLoad}， isReady- ${videoAd?.isReady} \n")
    }

    /**
     *  将所有的日志显示到textView上
     */
    private fun setLogsView(logs: String) {
        Log.i(TAG, logs)
        logStr.append(logs)
        if (logStr.isNotEmpty()) {
            binding.sigmobInfoTvLog.text = logStr.toString()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        videoAd?.destroy()
        interstitialAd?.destroy()
    }

    private var mExitTime: Long = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - mExitTime > 2000) {
                Toast.makeText(this, getString(R.string.exit_program), Toast.LENGTH_SHORT).show()
                mExitTime = System.currentTimeMillis()
            } else {
                if (!this.isFinishing) {
                    finishAndRemoveTask()
                }
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}